using System;
using System.Diagnostics.Contracts;

namespace OpenWaves.Caching
{
    [ContractClass(typeof(CacheContract<>))]
    public interface ICache<T> where T : class
    {
        void InsertValue(string key, T value, DateTime absoluteExpiration);
        void EvictValue(string key);
        T GetValue(string key);
    }

    [ContractClassFor(typeof(ICache<>))]
    abstract class CacheContract<T> : ICache<T> where T : class
    {
        public void InsertValue(string key, T value, DateTime absoluteExpiration)
        {
            Contract.Requires<ArgumentNullException>(String.IsNullOrEmpty(key) == false);
            throw new NotImplementedException();
        }

        public void EvictValue(string key)
        {
            Contract.Requires<ArgumentNullException>(String.IsNullOrEmpty(key) == false);
            throw new NotImplementedException();
        }

        public T GetValue(string key)
        {
            Contract.Requires<ArgumentNullException>(String.IsNullOrEmpty(key) == false);
            throw new NotImplementedException();
        }
    }

    public static class CacheExtensions
    {
        public static void InsertValue<T>(this ICache<T> cache, string key, T value, TimeSpan absoluteExpiration) where T : class
        {
            cache.InsertValue(key, value, absoluteExpiration == TimeSpan.MaxValue ? DateTime.MaxValue : Time.Now + absoluteExpiration);
        }
    }
}